<?php
    // +----------------------------------------------------------------------
    // | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
    // +----------------------------------------------------------------------
    // | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
    // +----------------------------------------------------------------------
    // | SiteUrl: http://www.luckyhhy.cn
    // +----------------------------------------------------------------------
    // | Author: LuckyHhy <jackhhy520@qq.com>
    // +----------------------------------------------------------------------
    // | Date: 2020/1/3-15:47
    // +----------------------------------------------------------------------
    // | Description:  
    // +----------------------------------------------------------------------


    namespace app\admin\controller;


    use app\admin\service\MenuService;
    use app\admin\model\Menus;

    use Jrk\Excel;
    use Jrk\Trees;
    use think\Exception;

    class Menu extends AdminBase
    {

        public function initialize()
        {
            parent::initialize(); // TODO: Change the autogenerated stub
            $this->service=new MenuService();
            $this->model=new Menus();
            $this->del="菜单";
        }


        /**
         * @return mixed
         * @throws Exception
         * @throws \think\exception\PDOException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/12 0012
         * @name: addMenu
         * @describe:
         */
        public function addMenu(){
            if (IS_POST){
                $data = $this->request->post();

                if (isset($data['id'])){
                    addLog("菜单修改操作","修改菜单：".$data['title']);
                }else{
                    addLog("菜单添加操作","新增菜单：".$data['title']);
                }
                return $this->model->addAndEdit($data);
            }
            $tree = Trees::toFormatTree($this->service->menuList());//获取菜单列表

            $pid = $this->request->param("id/d");//父id

            $this->assign(compact("pid", "tree"));

            return $this->fetch();
        }



        /**
         * @throws \PHPExcel_Reader_Exception
         * @throws \PHPExcel_Writer_Exception
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/3/17
         * @name: test
         * @describe:数据导出
         */
        public function test(){
            $list=Menus::where("status",1)->select()->toArray();
            $export = [];
            foreach ($list as $k){
                $export[] = [
                    $k['title'],$k['icon'],$k['href']
                ];
            }
            Excel::setExcelHeader(['菜单标题','图标','地址'])
                ->setExcelTile('资金监控', '资金监控',date('Y-m-d H:i:s',time()))
                ->setExcelContent($export)
                ->ExcelSave();
        }

        /**
         * @return mixed
         * @author: LuckyHhy <jackhhy520@qq.com>
         * @date: 2020/1/9
         * @name: editMenu
         * @describe:编辑
         */
        public function editMenu(){
            $tree = Trees::toFormatTree($this->service->menuList());//获取菜单列表
            //dump($tree);die;
            $id   = $this->request->param("id/d");//父id

            $info = $this->service->getOne(['id'=>$id]);

            if (empty($info)){
                $this->assign('msg','当前id:'.$id.'错误');
                $this->fetch("public/404");
            }

            $pid  = $info['pid'];


            $this->assign(compact("pid", "tree", "info"));
            return $this->fetch();
        }




    }